﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Ventigrate.Shared.AnonymousOfficeFileOpener.Modules
{
    public class AnonymousOfficeFileOpenerModule : IHttpModule
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.PostAuthenticateRequest += context_PostAuthenticateRequest;
        }

        protected void context_PostAuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;

            // Anonymous OPTIONS request from Office
            if (!application.User.Identity.IsAuthenticated &&
                context.Request.HttpMethod == "OPTIONS" &&
                context.Request.UserAgent.StartsWith("Microsoft Office"))
            {
                context.Response.StatusCode = 404;
                context.Response.End();
            }
        }
    }
}
